package de.karlNet.sambamanager.ldapController;

import java.util.ArrayList;
import java.util.List;

import javax.naming.directory.SearchControls;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.stereotype.Controller;

import de.karlNet.sambamanager.ldapModel.LdapObject;

@Controller
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class DnsTree extends AbstractLdapTree {
	@Autowired
	@Qualifier("ldapTemplate")
	private LdapTemplate ldapTemplate;

	@Override
	protected LdapTemplate getLdapTemplate() {
		return this.ldapTemplate;
	}

	public DnsTree() {
		
	}
	@Override
	public String getBaseDN() {
		String baseDN = super.getBaseDN();
		baseDN = "CN=MicrosoftDNS,DC=DomainDnsZones," + baseDN;
		return baseDN;
	} 
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<LdapObject> searchFor(String searchTerm) {
		List ldapObjects = getLdapTemplate().search(
				this.getBaseDN(),
				"(|(dc=" + searchTerm + "*)(dn=" + searchTerm + "*))", SearchControls.SUBTREE_SCOPE,
				this.attributesMapper);
		List<LdapObject> ret = new ArrayList<LdapObject>(ldapObjects);
		return ret;
	}
}
